program constraint_inside;

class date_t;
    rand bit [4:0] day;
    rand bit [3:0] month;
    rand bit [11:0] year;
    
    // 约束块是类的成员, 在类中具有唯一的名称
    constraint valid_date {
        day >= 1;
        month inside {[1:12]};
        year >= 1900;

        // Implication constraint: ->
        month inside {1, 3, 5, 7, 8, 10, 12} -> day <= 31;
        month inside {4, 6, 9, 11} -> day <= 30;
        (month == 2) -> (
            ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ?
                day <= 29 :
                day <= 28
        );

    }

endclass

initial begin
    date_t d = new();
    repeat (10) begin
        // randomize() 用于对类中的 rand 和 randc 变量进行随机化，其会返回一个表示随机化是否成功的布尔值
        // 内联约束: 在 randomize() 调用中添加内联约束
        if (d.randomize() with { year <= 2999; }) begin
            $display("Random date: %0d/%0d/%0d", d.month, d.day, d.year);
        end else begin
            $display("Failed to randomize date");
            $finish;
        end
    end
end

endprogram
